﻿using Aop.Api.Encryption;
using System;
using System.BLL;
using System.Collections.Generic;
using System.DAL;
using System.DataInitialization.ModelsData;
using System.Dto;
using System.IBLL;
using System.IDAL;
using System.IO;
using System.Linq;
using System.Models;
using System.Models.Models;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace System.DataInitialization
{
    class Program
    {

        static async Task Main(string[] args)
        {
            #region 角色表数据初始化
            /*DepartmentRoleData roleData = new DepartmentRoleData();
            roleData.AddData();
            Console.WriteLine("DepartmentRoleData , 生成成功！");*/
            #endregion

            #region 员工表数据初始化

            /*StaffData staffData = new StaffData();
            staffData.AddData();*/

            // 测试密码加密解密效果  990905
            /*MyDbContext db = new MyDbContext();
            var data = db.Staffs.Find(1);
            string key = "ILIVEZENGZHIHUIL";
            CryptoHelper helper = new CryptoHelper(key);
            string pass = helper.Decrypt(data.PassWord);
            Console.WriteLine(pass); // 输出了990905*/
            #endregion

            #region 菜单表数据初始化
            /*MenusData menusData = new MenusData();
            menusData.AddData();*/
            #endregion

            #region 权限表 超级管理员权限数据初始化
            /*JurisdictionData jurisdictionData = new JurisdictionData();
            jurisdictionData.AddData();*/
            #endregion

            /*ISystemManager Svc = new SystemManager();
            await Svc.AddLogs("页面操作", "新增了菜单页面", "卢俊宇");*/
            /*MyDbContext db = new MyDbContext();
            var menusId = db.Jurisdictions.Where(m => m.DepartmentRoleId == 1).Select(m => m.MenusId).ToList();
            var data = db.Menuss.Select(m => new Listdto()
            {
                MenusId = m.Id,
                MenusName = m.Title,
                IsCheck = menusId.Contains(m.Id) ? true : false
            }).ToList();

            foreach(var list in data)
            {
                Console.WriteLine($"id是{list.MenusId}，菜单名是{list.MenusName},状态是{list.IsCheck}");
            }*/

            /*MyDbContext db = new MyDbContext();
            CryptoHelper helper = new CryptoHelper();
            var data = db.Households.ToList();
            foreach(var list in data)
            {
                list.PassWord = helper.Encrypt("123456");
                
            }
            db.SaveChanges();
            Console.WriteLine("密码修改成功");*/
            /*
                        var data1 = DateTime.Now.AddDays(-1).Date;

                        Console.WriteLine(data1);
                        var data2 = DateTime.Now;
                        Console.WriteLine(data2);
                        var data5 = DateTime.Now.Add(data2 - data1);
                        //var data3 = int.Parse((data2 - data1).ToString()) < 1;
                        Console.WriteLine(data5);
                        var data6 = data2 > data1;
                        Console.WriteLine(data6);*/

            WebRequest request = WebRequest.Create("https://api.pwmqr.com/qrcode/create/?url='123'");
            request.Method = "Get";
            WebResponse response = request.GetResponse();
            Stream reader = response.GetResponseStream();

            FileStream writer = new FileStream(@"E:\.temp\1.png",FileMode.OpenOrCreate,FileAccess.Write);
            byte[] buff = new byte[512];
            int c = 0; //实际读取的字节数
            while ((c = reader.Read(buff, 0, buff.Length)) > 0)
            {
                writer.Write(buff, 0, c);
            }
            writer.Close();
            writer.Dispose();
            reader.Close();
            reader.Dispose();
            response.Close();


            /*            var request = new HttpRequestMessage(HttpMethod.Get, "https://api.pwmqr.com/qrcode/create/?url='123'");*/

        }

        class Listdto
        {
            public int MenusId { get; set; }
            public string MenusName { get; set; }
            public bool IsCheck { get; set; }
        }

        static List<Menuslists> GetMenuslists(int RoleId)
        {
            /*using (MyDbContext db = new MyDbContext())
            {
                var MenusIdLists = (from i in db.Jurisdictions where i.DepartmentRoleId == RoleId select i.MenusId).ToList();
                List<Menuslists> menuslists = new List<Menuslists>();
                List<Menus> MenusListDatas = new List<Menus>();
                foreach (var MenusId in MenusIdLists)
                {
                    using (IMenusService systemSvc = new MenusService())
                    {
                        var MenusListData = await systemSvc.GetOneByIdAsync(MenusId);
                        MenusListDatas.Add(MenusListData);
                        var data = buildMenuChild(MenusListDatas);
                        menuslists = data;
                    }
                }
                return menuslists;

            }*/

            using (IJurisdictionService jurisSvc = new JurisdictionService())
            {
                var JurisdictionLists = jurisSvc.GetAllAsync().ToList();
                var MenusIdLists = (from i in JurisdictionLists where i.DepartmentRoleId == RoleId select i.MenusId).ToList();
                List<Menuslists> menuslists = new List<Menuslists>();
                List<Menus> MenusListDatas = new List<Menus>();
                foreach (var MenusId in MenusIdLists)
                {
                    using (IMenusService systemSvc = new MenusService())
                    {
                        var MenusListData = systemSvc.GetOneByIdAsync(MenusId);
                        MenusListDatas.Add(MenusListData);
                        var data = buildMenuChild(MenusListDatas);
                        menuslists = data;
                    }
                }
                return menuslists;
            }
        }

        static List<Menuslists> buildMenuChild(List<Menus> menus)
        {
            using (IMenusService systemSvc = new MenusService())
            {
                List<Menuslists> treeList = new List<Menuslists>();

                foreach (var list in menus)
                {
                    if (list.Pid == null)
                    {
                        Menuslists nodetmp = new Menuslists()
                        {
                            title = list.Title,
                            icon = list.Icon,
                            href = list.Href,
                            target = list.Target
                        };
                        nodetmp.child = systemSvc.GetAllAsync().Where(m => m.Pid == list.Id).Select(m => new Menuslists()
                        {
                            title = m.Title,
                            icon = m.Icon,
                            href = m.Href,
                            target = m.Target
                        }).ToList();
                        treeList.Add(nodetmp);

                    }
                }
                return treeList;
            }



        }
    }
}
